package com.example.mediaplayerdemo.main.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.drawerlayout.widget.DrawerLayout;

import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

import com.example.mediaplayerdemo.broadcast.fragment.BroadcastFragment;
import com.example.mediaplayerdemo.chart.fragment.ChartFragment;
import com.example.mediaplayerdemo.R;
import com.example.mediaplayerdemo.gestures.fragment.GesturesFragment;
import com.example.mediaplayerdemo.main.fragment.MainFragment;
import com.example.mediaplayerdemo.videoplay.fragment.VideoPlayFragment;
import com.google.android.material.navigation.NavigationView;

public class MainActivity extends AppCompatActivity{
    private Toolbar toolbar;
    private DrawerLayout drawerLayout;
    private NavigationView navigationView;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id.toolbar);
        drawerLayout = findViewById(R.id.drawerlayout);
        navigationView = findViewById(R.id.navigation);
        setSupportActionBar(toolbar);   //替换ActionBar
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);  //返回键
        getSupportActionBar().setHomeButtonEnabled(true);   //返回键可用

//      将侧滑栏和工具栏绑定

//        创建切换对象toggle
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };

        toggle.syncState();     //同步
        drawerLayout.addDrawerListener(toggle);

//      初始化显示fragment
        getSupportFragmentManager().beginTransaction()
                .add(R.id.framelayout, MainFragment.newInstance("", ""), "")
                .commit();
        toolbar.setTitle("首页");

//        侧滑菜单选择事件，根据不同选项显示fragment
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                item.setCheckable(true);
                switch (item.getItemId()) {
                    case R.id.ItemMain:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, MainFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("首页");
                        break;
                    case R.id.ItemGestures:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, GesturesFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("手势");
                        break;
                    case R.id.ItemVideo:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, VideoPlayFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("多媒体");
                        break;
                    case R.id.ItemChart:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, ChartFragment.newInstance("", ""), "")
                                .commit();
                        toolbar.setTitle("图表");
                        break;
                    case R.id.ItemBroadcast:
                        getSupportFragmentManager().beginTransaction()
                                .replace(R.id.framelayout, BroadcastFragment.newInstance("" ,""), "")
                                .commit();
                        toolbar.setTitle("广播");
                        break;
                    case R.id.ItemExit:
                        finish();
                        break;
                }
                return false;
            }
        });
    }
}